<template>
	<view class="login">
		<view class="bg"></view>
		<view class="form_box">
			<view class="form_bo_wrap">
				<u-subsection :list="userList" :current="curNow" @change="sectionChange" class="user"></u-subsection>

				<u--form labelPosition="left" :model="model1" :rules="rules" ref="form1">
					<u-form-item label="用户名:" prop="userInfo.userName" borderBottom ref="item1">
						<u--input v-model="model1.userInfo.userName" placeholder="请输入用户名" border="none"></u--input>
					</u-form-item>

					<u-form-item label="密  码:" prop="userInfo.password" borderBottom ref="item1">
						<u--input v-model="model1.userInfo.password" type="password" placeholder="请输入密码" border="none"></u--input>
					</u-form-item>
				</u--form>
				<u-button type="primary" text="登录" customStyle="margin-top: 16px" @click="submit"></u-button>
				<view class="enroll" @click="onLogin">注册用户</view>
				<!--<view class="forget">忘记密码？</view>-->
			</view>
		</view>
	</view>
</template>

<script>
import {doLogin} from '../../common/api'
export default {
	data() {
		return {
			userList: ['教练员登录', '学员登录'],
			curNow: 0,
			model1: {
				userInfo: {
					userName: '',
					password: '',
					userType: 'coach', //trainee:学员 coach:教练
				},
			},
			rules: {
				'userInfo.userName': {
					type: 'string',
					required: true,
					message: '请输入用户名',
					trigger: ['blur', 'change']
				},
				'userInfo.password': {
					type: 'string',
					required: true,
					message: '请输入密码',
					trigger: ['blur', 'change']
				},
			},
		}
	},
	onLoad() {

	},
	methods: {
   	submit() {
			// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
			this.$refs.form1.validate().then(async res => {
				// uni.$u.toast('登录成功')
				if(this.curNow == 0){
					this.model1.userInfo.userType = 'coach'
					uni.setStorageSync('userType', 'coach')
				} else {
					this.model1.userInfo.userType = 'trainee'
					uni.setStorageSync('userType', 'trainee')
				}
				
				let data = await doLogin(this.model1.userInfo)
				console.log(res, data);
				if(data){
					uni.switchTab({
						url: '/pages/home/home'
					});
				}
				uni.setStorageSync('userData', data)
			}).catch(errors => {
				uni.$u.toast('登录失败')
			})
		},
		onLogin() {
			uni.navigateTo({
				url: '/pages/enroll/enroll'
			});
		},
		sectionChange(index) {
			this.curNow = index;
		},
	}
}
</script>

<style lang="scss" scoped>
.login {
	.bg {
		width: 100%;
		height: 400rpx;
		background: url('../../static/images/dcjx.jpg') no-repeat;
		background-size: 100% 100%;
	}

	.form_box {
		width: 100%;
		position: absolute;
		top: 280rpx;

		.form_bo_wrap {
			width: 640rpx;
			margin: 0 auto;
			padding: 32rpx;
			box-shadow: 0px 0px 5px #333333;
			border-radius: 20rpx;
			background: #fff;

			.user {
				margin-bottom: 30rpx;
			}
		}
	}

	.enroll {
		font-size: $uni-font-size-mini;
		margin-top: 25rpx;
	}

	.forget {
		font-size: $uni-font-size-mini;
		color: $uni-color-type1;
		text-align: right;
		margin: 20rpx 0 20rpx;
	}
}
</style>
